package com.dynamic.mybatis.web.security;

import com.dynamic.mybatis.core.intercept.DynamicSqlSessionExecuteHandler;
import com.dynamic.mybatis.core.metadata.DynamicMappedStatement;
import com.dynamic.mybatis.core.session.DynamicSqlSessionTemplate;
import com.dynamic.mybatis.core.utils.BeanUtils;
import com.dynamic.mybatis.core.utils.StringUtils;

import java.util.Map;

/**
 * 　* @Description: 取缓存参数并合并到参数中
 */
public class SecurityParameterHandler   implements DynamicSqlSessionExecuteHandler {
    DynamicSqlSessionTemplate dynamicSqlSessionTemplate;
    public SecurityParameterHandler(DynamicSqlSessionTemplate dynamicSqlSessionTemplate) {
        this.dynamicSqlSessionTemplate = dynamicSqlSessionTemplate;
    }

    @Override
    public Object before(DynamicMappedStatement dms, Object p) {
        if (!(p instanceof SecuritySessionContext)) {
            return p;
        }

        SecuritySessionContext parameter = (SecuritySessionContext) p;
        Object param = parameter.getContext();
        if (StringUtils.isNotEmpty(parameter.getToken())) {
            Object p1 = SecuritySessionContextHolder.get(parameter.getToken());
            if (parameter != null && parameter instanceof Map && p1 != null && p1 instanceof Map) {
                param = BeanUtils.copy(p1, parameter);
            }
        }
        return param;
    }


    @Override
    public Object after(DynamicMappedStatement dms, Object parameter, Object result) {
        return result;
    }
}
